package com.mangowang.module.main.ui;

/**
 * 文件名 : EditUserInfoActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/27 14:51
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import androidx.fragment.app.FragmentActivity;

import com.google.protobuf.InvalidProtocolBufferException;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.mangowang.lib.common.base.BaseActivity;
import com.mangowang.lib.common.help.GlideHelper;
import com.mangowang.lib.common.help.PermissionHelper;
import com.mangowang.lib.common.help.UserHelper;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.MemberOuterClass;
import com.mangowang.lib.common.util.ImageHelper;
import com.mangowang.lib.common.util.StatusBarUtil;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class EditUserInfoActivity extends BaseActivity {

    @BindView(R2.id.back_iv)
    ImageView backIv;
    @BindView(R2.id.title_tv)
    TextView titleTv;
    @BindView(R2.id.btn_save)
    TextView btnSave;
    @BindView(R2.id.relativeLayout)
    RelativeLayout relativeLayout;
    @BindView(R2.id.avatar_iv)
    ImageView avatarIv;
    @BindView(R2.id.change_avatar_rlt)
    RelativeLayout changeAvatarRlt;
    @BindView(R2.id.et_true_name)
    EditText etTrueName;

    @BindView(R2.id.et_qr_card)
    EditText etQrCard;
    RelativeLayout rlZhanYeArea;
    private String compressPath;
    private String areaId = "";
    private Unbinder bind;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_user_info);
        bind = ButterKnife.bind(this);
        titleTv.setText("个人资料");
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        etTrueName.setText(UserHelper.getUser().getTruename());
        etQrCard.setText(UserHelper.getUser().getIdcard());
        GlideHelper.INSTANCE.loadCircleImage(mContext,avatarIv, UserHelper.getUser().getMember_avatar());
        backIv.setOnClickListener(v -> {
            finish();
        });
        btnSave.setOnClickListener(v -> {save();
        });
        changeAvatarRlt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionHelper.getInstance().getPermission((FragmentActivity) mContext, new PermissionHelper.PermissionCallBack() {
                    @Override
                    public void onSuccess() {
                        ImageHelper.showAvatarChoose(EditUserInfoActivity.this);
                    }

                    @Override
                    public void onFail() {
                        showToast("请先允许权限再使用此功能");
                    }
                }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
        });
    }


    private void save() {
        if (compressPath != null) {
            upLoadImage();
        } else {
            submit(UserHelper.getUser().getMember_avatar());
        }
    }

    private void upLoadImage() {
        showProgress();
        ImageHelper.upLoadImage(compressPath, new ImageHelper.OnUpLoadImageCallBack() {
            @Override
            public void onSuccess(String images) throws InvalidProtocolBufferException {
                String[] temp = images.split(",");
                byte[] bytes = new byte[temp.length];
                for (int i = 0; i < temp.length; i++) {
                    bytes[i] = (byte) Integer.parseInt(temp[i]);
                }
                hideProgress();
                submit( MemberOuterClass.img_upload.parseFrom(bytes).getImg());
            }

            @Override
            public void onFail(String error) {
                hideProgress();
                showToast(error);
            }
        });
    }

    private void submit(String member_avatar) {
        MemberOuterClass.Member.Builder builder = MemberOuterClass.Member.newBuilder();
        builder.setMemberAvatar(member_avatar);
        showProgress();
        NetworkManager.INSTANCE.post(Apis.editMemberDataInfo, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) {

            }

            @Override
            public void onError(int code, String errorMessage) {
                if (code == 200) {
                    hideProgress();
                    showToast("修改成功");
                    setResult(RESULT_OK);
                    finish();
                } else {
                    hideProgress();
                    showToast(errorMessage);
                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    List<LocalMedia> localMedias = PictureSelector.obtainMultipleResult(data);
                    compressPath = localMedias.get(0).getCompressPath();
                    GlideHelper.INSTANCE.loadCircleImage(mContext,avatarIv, compressPath);
                    break;
                case 200:
                    String area_name = data.getStringExtra("area_name");
                    areaId = data.getStringExtra("areaId");

                    break;
            }
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }
}
